package com.insist.controller;

import com.insist.entity.ProductInfo;
import com.insist.model.ClassInfoModel;
import com.insist.model.OrderDetailInfoModel;
import com.insist.service.OrderService;
import com.insist.service.ProductService;
import com.insist.util.CmmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by INSIST on 2016/6/28.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/findAllClass")
    @ResponseBody
    public List<ClassInfoModel> findAllClass(){
        return productService.findAllClass();
    }

    @RequestMapping("/findHotProductList")
    @ResponseBody
    public List<ProductInfo> findHotProductList(){
        List<ProductInfo> list =productService.findHotProduct();
        for(ProductInfo info:list){
            info.setPicture(CmmUtils.replacePictureUrl(info.getPicture()));
        };
        return list;
    }


    @RequestMapping("/getProductById")
    @ResponseBody
    public ProductInfo getProductById(Long id){
        ProductInfo info = productService.getProductById(id);
        info.setPicture(CmmUtils.replacePictureUrl(info.getPicture()));
        return info;
    }

    @RequestMapping("/findActorByProductId")
    @ResponseBody
    public List<OrderDetailInfoModel> findActorByProductId(Long productId){
        List<OrderDetailInfoModel> infos = orderService.findActorByProductId(productId);
        for(OrderDetailInfoModel info:infos){
            info.setProductImage(CmmUtils.replacePictureUrl(info.getProductImage()));
        };
        return infos;
    }

    @RequestMapping("/findProducts")
    @ResponseBody
    public List<ProductInfo> findProducts(@RequestParam(required = false)Long classId,@RequestParam(required = false)Long brandId,
                                          @RequestParam(required = false)Double startPrice,@RequestParam(required = false)Double endPrice){
        List<ProductInfo> list = productService.findProducts(classId, brandId,startPrice,endPrice);
        for(ProductInfo info:list){
            info.setPicture(CmmUtils.replacePictureUrl(info.getPicture()));
        };
        return list;
    }
}
